package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.PaymentBillQueryObject;
import cn.wolfcode.crm.service.IPaymentBillService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("paymentBill")
public class PaymentBillController {
    @Autowired
    private IPaymentBillService service;

    @RequestMapping("")
    @PermissionName("缴费处理首页")
    @RequiresPermissions("paymentBill:index")
    public String index() {
        return "paymentBill/list";
    }

    @RequestMapping("list")
    @PermissionName("缴费处理列表")
    @RequiresPermissions("paymentBill:list")
    @ResponseBody
    public PageResult list(PaymentBillQueryObject qo) {
        return service.query(qo);
    }


    /**
     * 缴费功能实现
     *
     * @param id        当前缴费人员的id
     * @param begindate 缴费人员的起保时间
     * @param payWayId  缴费的支付方式
     * @return 返回执行的结果(成功或者失败)
     */
    @RequestMapping("pay")
    @PermissionName("缴费处理缴费功能")
    @RequiresPermissions("paymentBill:pay")
    @ResponseBody
    public JsonResult pay(Long id, String begindate, Long payWayId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JsonResult jsonResult = new JsonResult();
        try {
            Date beginDate = sdf.parse(begindate);
            try {
                service.pay(id, beginDate, payWayId);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.markMsg("缴费失败");
            }
            return jsonResult;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
